#include "extractorShapeVector.h"
#include "grid.h"


using namespace cv;
using namespace std;

extractorShapeVector::extractorShapeVector(IplImage *_image, char* _labelImage, Slic _clustering){
	this->size = _clustering.getCenter_counts().size();
	this->labelImage = _labelImage;
	int _nB = 8;
	//Mat clusters = _clustering.convertClustersToMat();
	//grid gridforcluster(clusters,_nB,_labelImage);
	double sum = 0.0;
	
	// running cross clusters
	for(int icluster = 0; icluster < size; icluster++){
		shapeVector _vector(_nB*_nB,icluster,_labelImage);
		vector<CvPoint> _contour = _clustering.get_Contours_Superpixel(_image,icluster);
		cv::Rect _roi = _clustering.get_Rectangle_Boundary_Contour(_contour);
		grid grid_for_superpixel(_roi,_nB,_labelImage);
		
		for(int igrid = 0; igrid < (_nB *_nB);igrid++){
			gridItem _gridItem = grid_for_superpixel.getItem(igrid);

			for (int i = 0; i < (int)_contour.size(); i++) {
				Point p = _contour[i];
				if(_gridItem.checkPixelInBound(p.x,p.y)){
							sum = sum + 1.0;
				}	
			}

			double ratio = sum / (double)(_gridItem.getWidth() * _gridItem.getHeight());
			if(ratio > 1){
				cout << "Yield of square: " <<(double)(_gridItem.getWidth() * _gridItem.getHeight()) << endl;
				//cout << "Number of pixel: " << sum << endl;
				cout << "At cluster: " << icluster << endl;
				//cout << "At square: " << igrid << endl;
				//cout << "x BoundX y BoundY: " << _gridItem.getX()<< " " << _gridItem.getBoundX()<< " " << _gridItem.getY()<< " " << _gridItem.getBoundY()<< endl;
				//cout << "cheking...." << endl;
				//cout << endl;
			}
			_vector.addValue(ratio);
			sum = 0.0;
		}
		this->listVector.push_back(_vector);
	}
}

vector<shapeVector> extractorShapeVector::getListVector() {
	return this->listVector;
}